package com.miukoo.aikao.qexam.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.miukoo.aikao.core.dtos.*;
import com.miukoo.aikao.core.pojos.SecurityUser;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.dtos.ExamDto;
import com.miukoo.aikao.core.utils.AppUserUtils;
import com.miukoo.aikao.qexam.dto.request.ExamSaveReqDto;
import com.miukoo.aikao.qexam.dto.response.ExamOnlineRespDto;
import com.miukoo.aikao.qexam.dto.response.ExamReviewRespDto;
import com.miukoo.aikao.qexam.pojos.Exam;
import com.miukoo.aikao.qexam.service.ExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
* <p>
* 考试控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-07-25 16:18
*/
@Api(tags={"考试"})
@RestController
@RequestMapping("/exam/api/exam/exam")
public class ExamController {

    @Autowired
    private ExamService baseService;

    /**
    * 添加或修改
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiResult save(@RequestBody ExamSaveReqDto reqDto) {
        //复制参数
        baseService.save(reqDto);
        return ApiUtils.success();
    }

    /**
    * 批量删除
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult edit(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        baseService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
    * 查找详情
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiResult<ExamDto> find(@RequestBody BaseIdReqDto reqDto) {
        ExamSaveReqDto dto = baseService.findDetail(reqDto.getId());
        return ApiUtils.success(dto);
    }

    /**
     * 查找详情
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/state", method = { RequestMethod.POST})
    public ApiResult state(@RequestBody BaseStateReqDto reqDto) {

        QueryWrapper<Exam> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Exam::getId, reqDto.getIds());
        Exam exam = new Exam();
        exam.setState(reqDto.getState());
        exam.setUpdateTime(new Date());

        baseService.update(exam, wrapper);
        return ApiUtils.success();
    }


    /**
     * 分页查找
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "考试视角")
    @RequestMapping(value = "/online-paging", method = { RequestMethod.POST})
    public ApiResult<IPage<ExamOnlineRespDto>> myPaging(@RequestBody PageReqDto<ExamDto> reqDto) {
        SecurityUser user = AppUserUtils.getUser();
        IPage<ExamOnlineRespDto> page = baseService.onlinePaging(reqDto, user.getId());
        return ApiUtils.success(page);
    }

    /**
    * 分页查找
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiResult<IPage<ExamDto>> paging(@RequestBody PageReqDto<ExamDto> reqDto) {

        //分页查询并转换
        IPage<ExamDto> page = baseService.paging(reqDto);

        return ApiUtils.success(page);
    }


    /**
     * 分页查找
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "待阅试卷")
    @RequestMapping(value = "/review-paging", method = { RequestMethod.POST})
    public ApiResult<IPage<ExamReviewRespDto>> reviewPaging(@RequestBody PageReqDto<ExamDto> reqDto) {

        //分页查询并转换
        IPage<ExamReviewRespDto> page = baseService.reviewPaging(reqDto);

        return ApiUtils.success(page);
    }


}
